home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 3 #12 / IMG 33 Dec 1995.iso / Important Files / MGW1Codeƒ / MGWGraphics1.c < prev    next >
Text File  |  1995-06-26  |  7KB  |  201 lines

  1. //==============================================================================================\\
  2. //        -----------------------------------------------------------------------------------        \\
  3. //        MGWGraphics1.c version 1.0.0    copyright © 1993…1995 Jamie McCornack, john calhoun        \\
  4. //        -----------------------------------------------------------------------------------        \\
  5. //         Graphics utilities for Macintosh GameWriter 1.0.0, a training program…                    \\
  6. //        …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,…        \\
  7. //        …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment…    \\
  8. //        …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc.    \\
  9. //         A tutorial is available in Tricks of the Mac Game Programming Gurus, published…        \\
  10. //        …by Hayden Books, August 1995.                                                                        \\
  11. //                                                                                                \\
  12. //        This code is offered by the copyright holders for no fee and for whatever use…            \\
  13. //        …you care to make of it, but we do hope you remember where it came from.                \\
  14. //                                                                                                \\
  15. //        Please send bug reports to MacGameDev at America OnLine.    macgamedev@aol.com            \\
  16. //        Suggestions and observations are also appreciated.                                        \\
  17. //        Updates and upgrades will be available now and then from the above e-mail address.        \\
  18. //==============================================================================================\\
  19.  
  20.  
  21. #include "MGWExterns1.h"
  22.  
  23. extern GDHandle    gThisGDevice;
  24. extern GrafPtr  mainWindow, workCPort;    // Your program may use different names.
  25.                                         // See comments, DumpScreenToWork().
  26.                                         
  27. //==============================================================  Functions
  28.  
  29. //--------------------------------------------------------------  CreateOffScreenBitMap
  30.  
  31. // Checks for sufficient memory, then creates an offscreen bitmap.
  32. // If there is not sufficient memory, alerts the user and quits to Finder.
  33.  
  34. void CreateOffScreenBitMap (Rect *theRect, GrafPtr *offScreen)
  35. {
  36.     GrafPtr        theBWPort;
  37.     BitMap        theBitMap;    
  38.     long        theRowBytes;
  39.     
  40.     theBWPort = (GrafPtr)(NewPtr(sizeof(GrafPort)));
  41.     OpenPort(theBWPort);
  42.     theRowBytes = (long)((theRect->right - theRect->left + 15L) / 16L) * 2L;
  43.     theBitMap.rowBytes = (short)theRowBytes;
  44.     theBitMap.baseAddr = NewPtr((long)theBitMap.rowBytes * 
  45.         (theRect->bottom - theRect->top));
  46.     if (theBitMap.baseAddr == 0L)
  47.         RedAlert(kErrLowMemory);
  48.     theBitMap.bounds = *theRect;
  49.     if (MemError() != noErr)
  50.         RedAlert(kErrLowMemory);
  51.     SetPortBits(&theBitMap);
  52.     ClipRect(theRect);
  53.     RectRgn(theBWPort->visRgn, theRect);
  54.     EraseRect(theRect);
  55.     *offScreen = theBWPort;
  56. }
  57.  
  58. //--------------------------------------------------------------  CreateOffScreenPixMap
  59.  
  60. // Handles the creation of an offscreen pixmap.  Depth is assumed to be that of the…
  61. // current gDevice.  If the allocation fails (low memory, etc.) we quit to Finder.
  62.  
  63. void CreateOffScreenPixMap (Rect *theRect, CGrafPtr *offScreen)
  64. {
  65.     CTabHandle    thisColorTable;
  66.     GDHandle    oldDevice;
  67.     CGrafPtr    newCGrafPtr;
  68.     Ptr            theseBits;
  69.     long        sizeOfOff, offRowBytes;
  70.     OSErr        theErr;
  71.     short        thisDepth;
  72.     
  73.     gThisGDevice = GetMainDevice();
  74.     oldDevice = GetGDevice();
  75.     SetGDevice(gThisGDevice);
  76.     newCGrafPtr = 0L;
  77.     newCGrafPtr = (CGrafPtr)NewPtrClear(sizeof(CGrafPort));
  78.     if (newCGrafPtr != 0L)
  79.     {
  80.         OpenCPort(newCGrafPtr);
  81.         thisDepth = (**(*newCGrafPtr).portPixMap).pixelSize;
  82.         offRowBytes = ((((long)thisDepth * 
  83.                 (long)(theRect->right - theRect->left)) + 15L) >> 4L) << 1L;
  84.         sizeOfOff = (long)(theRect->bottom - theRect->top) * offRowBytes;
  85.         OffsetRect(theRect, -theRect->left, -theRect->top);
  86.         theseBits = NewPtr(sizeOfOff);
  87.         if (theseBits != 0L)
  88.         {
  89.             (**(*newCGrafPtr).portPixMap).baseAddr = theseBits;
  90.             (**(*newCGrafPtr).portPixMap).rowBytes = (short)offRowBytes + 0x8000;
  91.             (**(*newCGrafPtr).portPixMap).bounds = *theRect;
  92.             thisColorTable = (**(**gThisGDevice).gdPMap).pmTable;
  93.             theErr = HandToHand((Handle *)&thisColorTable);
  94.             (**(*newCGrafPtr).portPixMap).pmTable = thisColorTable;
  95.             ClipRect(theRect);
  96.             RectRgn(newCGrafPtr->visRgn, theRect);
  97.             ForeColor(blackColor);
  98.             BackColor(whiteColor);
  99.             EraseRect(theRect);
  100.         }
  101.         else
  102.         {
  103.             CloseCPort(newCGrafPtr);        
  104.             DisposePtr((Ptr)newCGrafPtr);
  105.             newCGrafPtr = 0L;
  106.             RedAlert(kErrLowMemory);
  107.         }
  108.     }
  109.     else
  110.         RedAlert(kErrLowMemory);
  111.     
  112.     *offScreen = newCGrafPtr;
  113.     SetGDevice(oldDevice);
  114. }
  115.  
  116. //--------------------------------------------------------------  KillOffScreenBitMap
  117.  
  118. // Disposes of an offscreen bitmap, and frees the memory for other purposes.
  119.  
  120.  
  121.     void KillOffscreenBitMap (GrafPtr *wasPort)
  122.  
  123.     {
  124.         if (wasPort != nil)
  125.             {
  126.                 ClosePort(*wasPort);
  127.                 *wasPort = nil;
  128.             }
  129.     }
  130.  
  131. //--------------------------------------------------------------  KillOffScreenPixMap
  132.  
  133. // Disposes of an offscreen pixMap, and frees the memory for other purposes.
  134.  
  135.  
  136.     void KillOffscreenPixMap (CGrafPtr *wasPort)
  137.  
  138.     {
  139.         if (wasPort != nil)
  140.             {
  141.                 CloseCPort(*wasPort);
  142.                 *wasPort = nil;
  143.             }
  144.     }
  145.  
  146. //--------------------------------------------------------------  LoadGraphic
  147.  
  148. // Handy function that loads a PICT graphic, get's its bounds and draws it.
  149. // The port drawn to is assumed the current port.  No scaling is done.
  150.  
  151. void LoadGraphic (short thePictID)
  152. {
  153.     Rect        bounds;
  154.     PicHandle    thePicture;
  155.     
  156.     thePicture = GetPicture(thePictID);            // Load graphic from resource fork.
  157.     if (thePicture == 0L)                        // Check to see if nil (did it load?)
  158.         RedAlert(kErrNoResource);
  159.     HLock((Handle)thePicture);                    // If we made it this far, lock handle.
  160.     bounds = (*thePicture)->picFrame;            // Get a copy of the picture's bounds.
  161.     HUnlock((Handle)thePicture);                // We can unlock the picture now.
  162.     OffsetRect(&bounds, -bounds.left, -bounds.top);    // Offset bounds rect to (0, 0).
  163.     DrawPicture(thePicture, &bounds);            // Draw picture to current port.
  164.     ReleaseResource((Handle)thePicture);        // Dispose of picture from heap.
  165. }
  166.  
  167. //--------------------------------------------------------------  DumpWorkToScreen
  168.  
  169. // Simple handy function that copies the entire work pixmap to the screen.
  170. // Of course, you'll have to rename the CopyBits parameters to fit your own program,…
  171. // …unless you habitually name your main window mainWindow and your offscreen…
  172. // …graphics buffer workCPort. Generally, you'll want to set targetRect to the full…
  173. // …clipping rect of the main window.
  174.  
  175.  
  176. void DumpWorkToScreen (Rect targetRect)
  177. {
  178.     CopyBits(&(((GrafPtr)workCPort)->portBits), 
  179.             &((GrafPtr)mainWindow)->portBits, 
  180.             &targetRect, &targetRect, srcCopy, 0L);
  181. }
  182.  
  183. //--------------------------------------------------------------  DumpScreenToWork
  184.  
  185. // Simple handy function that copies the entire screen to the work pixmap.
  186. // The reverse of the above routine. Very handy routines for when you have alerts,…
  187. // …or anything else that temporarily obscures the main window. Call DumpWorkToScreen()…
  188. // …just before the alert, and DumpScreenToWork immediately after the alert.
  189. // Oh yeah, and use the same targetRect for both routines.
  190.  
  191.  
  192. void DumpScreenToWork (Rect targetRect)
  193. {
  194.     CopyBits(&(((GrafPtr)mainWindow)->portBits), 
  195.             &((GrafPtr)workCPort)->portBits, 
  196.             &targetRect, &targetRect, srcCopy, 0L);
  197. }
  198.  
  199. //------------------------------------------------------------------------------------------\\
  200. //                                    End MGWGraphics1.c                                        \\
  201. //------------------------------------------------------------------------------------------\\